package padPack
{
	import com.bit101.components.Panel;
	
	import events.PushButtonSettingsEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	
	import globals.Primitive;
	import globals.TrueLoop;
	
	import org.audiofx.mp3.MP3FileReferenceLoader;
	import org.audiofx.mp3.MP3SoundEvent;
	
	import widgets.LoopSettings;

	public class TruePad extends Sprite
	{
		private var loopSettings:LoopSettings;
		private var selectedLoop:TrueLoop;
		public var pushButtons:Vector.<PadPushButton>;
		private var _width:int;
		private var frl:FileReferenceList;
		private var mp3loader:MP3FileReferenceLoader;
		private var keys:Array = new Array();
		[Embed(source="assets/pad_02.png", mimeType="image/png")] private const Background:Class;
		
		public function TruePad(width:int)
		{
			keys[0] = [49, 50, 51, 52];
			keys[1] = [81, 87, 69, 82];
			keys[2] = [65, 83, 68, 70];
			keys[3] = [90, 88, 67, 86];
			
			_width = width;
			pushButtons = new Vector.<PadPushButton>();
			
			loopSettings = new LoopSettings(this as TruePad);
			
			var sideBack:Bitmap = new Background();
			sideBack.x = TopLevel.stage.stageWidth - 430;
			addChild(sideBack);
			loopSettings.x = TopLevel.stage.stageWidth - 405;
			loopSettings.y = 25;
			addChild(loopSettings);
			
			//setting up file stuff
			frl = new FileReferenceList();
			loopSettings.addEventListener(LoopSettings.ADD_PRIMITIVE_BTN_CLICK, primitiveAddClick);
			frl.addEventListener(Event.SELECT, onSelect);
		}

		
		public function addSound(sound:Sound, name:String):void
		{
			var loop:TrueLoop;
			var prim:Primitive = new Primitive(sound, loop = new TrueLoop(null, null, name));
			
			var pushButton:PadPushButton = new PadPushButton(loop, sound.length);
			
			pushButton.x = (pushButtons.length % 4) * 145 + 10;
			pushButton.y = Math.floor(pushButtons.length / 4) * 145 + 10;
			
			pushButton.addEventListener(PushButtonSettingsEvent.CLICKED, buttonSettingsClicked);
			
			pushButtons.push(pushButton);
			
			pushButton.dispatchEvent(new PushButtonSettingsEvent(loop, PushButtonSettingsEvent.CLICKED));
			addChild(pushButton);
		}
		
		protected function buttonSettingsClicked(event:PushButtonSettingsEvent):void
		{
			loopSettings.loop = event.loop;
			selectedLoop = event.loop;
		}
		
		protected function onSelect(event:Event):void
		{
			frl.removeEventListener(Event.SELECT, onSelect);
			for each (var f:FileReference in frl.fileList)
			{
				mp3loader = new MP3FileReferenceLoader();
				mp3loader.addEventListener(MP3SoundEvent.COMPLETE,
					function onComplete(event:MP3SoundEvent):void
					{
						var sound:Sound = event.sound;
						var name:Array = f.name.split(".");
						addSound(sound, name[0]);
					});
				mp3loader.getSound(f);
			}
		}
		
		protected function primitiveAddClick(event:Event):void
		{
			var ff:FileFilter = new FileFilter("mp3 files only atm, sorry","*.mp3");
			frl.browse([ff]);
			frl.addEventListener(Event.SELECT, onSelect);
		}
	}
}